package com.dci.service.impl.schedule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dci.context.Config;
import com.dci.dao.pms.project.PmsItemInputMapper;
import com.dci.dao.pms.schedule.ProgressEnteringMapper;
import com.dci.dao.pms.schedule.ProgressTrackMapper;
import com.dci.model.project.PmsItemInput;
import com.dci.model.schedule.ProgressEntering;
import com.dci.model.schedule.ProgressTrack;
import com.dci.result.ResultInfo;
import com.dci.result.ResultUtil;
import com.dci.service.schedule.ProgressDetailsService;
import com.dci.service.schedule.ProgressTrackService;
import com.dci.util.Constant;
import com.dci.util.UUIDBuild;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.schedule
 * @Description: 项目计划管理
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class ProgressTrackServiceImpl implements ProgressTrackService {
    @Resource
    private ProgressTrackMapper progressTrackMapper;

    @Resource
    private PmsItemInputMapper pmsItemInputMapper;

    @Resource
    private ProgressEnteringMapper progressEnteringMapper;

    @Resource
    private ProgressDetailsService progressDetailsService;



    /**
     * 删除
     *
     * @param list
     * @return
     */
    @Override
    public ResultInfo delete(List<String> list) {
        List<String> idList=new ArrayList<>();
        for (int a = 0; a < list.size(); a++) {
            int i = progressTrackMapper.delete(list.get(a));
            String id=list.get(a);
            idList.add(id);
        }
        progressDetailsService.deleteByProjectId(idList);
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }



    /**
     * 修改
     *
     * @param progressTrack
     * @return
     */
    @Override
    public ResultInfo edit(ProgressTrack progressTrack) {
        int i = progressTrackMapper.update(progressTrack);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 911, null);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<ProgressTrack> select() {
        PmsItemInput pmsItemInput = new PmsItemInput();
        ProgressEntering progressEntering = new ProgressEntering();
        List<ProgressTrack> list = progressTrackMapper.select(null);
        List<ProgressTrack> list3 = new ArrayList<>();
        for (ProgressTrack progressTrack : list) {
            pmsItemInput.setProjectId(progressTrack.getId());
            progressEntering.setProjectId(progressTrack.getId());
            List<PmsItemInput> list1 = pmsItemInputMapper.selectByTimeAndId(pmsItemInput);
            List<ProgressEntering> list4 = progressEnteringMapper.selectByTimeAndId(progressEntering);
            if (list1.size() > 0) {
                double moneyNum = 0;
                double itemAmount = 0;
                for (int i = 0; i < list1.size(); i++) {
                    if (list1.get(i).getMoneyNum() != null) {
                        moneyNum += list1.get(i).getMoneyNum();
                    }
                }
                for (int a = 0; a < list4.size(); a++) {
                    if (list4.get(a).getItem_amount() != null) {
                        itemAmount += Double.parseDouble(list4.get(a).getItem_amount());
                    }
                }
                String use_amount = String.valueOf(moneyNum);
                String finance_amount = String.valueOf(itemAmount);
                progressTrack.setUse_amount(use_amount);
                progressTrack.setFinance_amount(finance_amount);
            }
            list3.add(progressTrack);
        }
        return list3;
    }

    /**
     * 通过时间查询
     *
     * @param track
     * @return
     */
    @Override
    public List<ProgressTrack> selectByTime(ProgressTrack track) {
        PmsItemInput pmsItemInput = new PmsItemInput();
        ProgressEntering progressEntering = new ProgressEntering();
        List<ProgressTrack> list = progressTrackMapper.selectTrack(track.getProject_team(),track.getConstruction_unit());
        List<ProgressTrack> list3 = new ArrayList<>();
        for (ProgressTrack progressTrack : list) {
            String id = progressTrack.getId();
            pmsItemInput.setProjectId(progressTrack.getId());
            pmsItemInput.setStartTime(track.getStartTime());
            pmsItemInput.setEndTime(track.getEndTime());

            progressEntering.setProjectId(progressTrack.getId());
            progressEntering.setStartTime(track.getStartTime());
            progressEntering.setEndTime(track.getEndTime());
            List<PmsItemInput> list1 = pmsItemInputMapper.selectByTimeAndId(pmsItemInput);
            List<ProgressEntering> list4 = progressEnteringMapper.selectByTimeAndId(progressEntering);
            List<String> stringList = new ArrayList<>();
            double moneyNum = 0;
            if (list1.size() > 0) {
                for (int i = 0; i < list1.size(); i++) {
                    // 使用金额统计
                    if(list1.get(i).getMoneyType().equals(Constant.PAYCAPITAL) || list1.get(i)
                            .getMoneyType().equals(Constant.PROGRESSCHANGE) ){
                        moneyNum += list1.get(i).getMoneyNum();
                    }
                }
                String use_amount = String.valueOf(moneyNum);
                if(!use_amount.equals(Constant.NUM)){
                    progressTrack.setUse_amount(use_amount);
                }
            }

            double itemAmount = 0;
            if (list4.size() > 0) {
                for (int a = 0; a < list4.size(); a++) {
                    if (list4.get(a).getItem_amount() != null) {
                        itemAmount += Double.parseDouble(list4.get(a).getItem_amount());
                    }
                }
                String finance_amount = String.valueOf(itemAmount);
                progressTrack.setFinance_amount(finance_amount);
            }
            if (list4.size() > 0) {
                double municipal_finance = 0;
                double district_finance = 0;
                double special_amount = 0;
                double other = 0;
                for (int a = 0; a < list4.size(); a++) {
                    if (list4.get(a).getMunicipal_finance() != null) {
                        municipal_finance += list4.get(a).getMunicipal_finance();
                    }
                    if (list4.get(a).getDistrict_finance() != null) {
                        district_finance += list4.get(a).getDistrict_finance();
                    }
                    if (list4.get(a).getSpecial() != null) {
                        special_amount += list4.get(a).getSpecial();
                    }
                    if (list4.get(a).getOther_finance() != null) {
                        other += list4.get(a).getOther_finance();
                    }
                }

                String municipal = null;
                String district = null;
                String special = null;
                String others = null;
                if (municipal_finance != 0) {
                    municipal = String.valueOf(municipal_finance);
                }
                if (district_finance != 0) {
                    district = String.valueOf(district_finance);
                }
                if (special_amount != 0) {
                    special = String.valueOf(special_amount);
                }
                if (other != 0) {
                    others = String.valueOf(other);
                }

                progressTrack.setGovMunicipal(municipal);
                progressTrack.setGovDistrict(district);
                progressTrack.setGovSpecial(special);
                progressTrack.setGovOther(others);
            }
            if (progressTrack.getMunicipal_finance() != null) {
                stringList.add("市财政");
            }
            if (progressTrack.getDistrict_finance() != null) {
                stringList.add("区财政");
            }
            if (progressTrack.getSpecial_amount() != null) {
                stringList.add("专项");
            }
            if (progressTrack.getOther() != null) {
                stringList.add("其他");
            }
            progressTrack.setList1(stringList);
            list3.add(progressTrack);
        }
        return list3;
    }

    /**
     * 工具方法
     *
     * @param object
     * @param type
     * @return
     */
    private String doValue(String object, String type) {
        String value;
        try {
            Map mapObject = (Map) JSON.parse(object);
            value = mapObject.get(type).toString();
        } catch (Exception ex) {
            value = "";
        }

        return value;
    }

    /**
     * 查询项目组
     *
     * @param project_team
     * @return
     */
    @Override
    public List<ProgressTrack> selectByProject_team(String project_team,String construction_unit) {

        List<ProgressTrack> list1 = progressTrackMapper.selectGetUnit(project_team,construction_unit);
        List<String> listString = new ArrayList<>();
        if (list1.size() > 0) {
            for (ProgressTrack progressTrack : list1) {
                listString.add(progressTrack.getConstruction_unit());
            }
        }
        List<ProgressTrack> list = progressTrackMapper.selectByProject_team(project_team,construction_unit);
        if (list.size() > 0) {
            for (ProgressTrack progressTrack1 : list) {
                progressTrack1.setProjectId(progressTrack1.getId());
                progressTrack1.setList(listString);
            }
        }

        return list;
    }

    /**
     * 通过id查询项目的资金类型
     *
     * @param id
     * @return
     */
    @Override
    public List<String> selectProjectById(String id) {
        List<ProgressTrack> list = progressTrackMapper.selectProjectById(id);
        ProgressTrack progressTrack = list.get(0);
        List<String> stringList = new ArrayList<>();
        if (progressTrack.getMunicipal_finance() != null && !"".equals(progressTrack.getMunicipal_finance())) {
            stringList.add("市财政");
        }
        if (progressTrack.getDistrict_finance() != null && !"".equals(progressTrack.getDistrict_finance())) {
            stringList.add("区财政");
        }
        if (progressTrack.getSpecial_amount() != null && !"".equals(progressTrack.getSpecial_amount())) {
            stringList.add("专项");
        }
        if (progressTrack.getOther() != null && !"".equals(progressTrack.getOther())) {
            stringList.add("其他");
        }
        return stringList;
    }

    @Override
    public List<ProgressTrack> queryByNameAndUnit(String project) {
        JSONObject jsonObject = JSONObject.parseObject(project);
        ProgressTrack progressTrack = JSONObject.toJavaObject(jsonObject, ProgressTrack.class);
        if (progressTrack != null) {
            if ("全部".equals(progressTrack.getConstruction_unit())) {
                progressTrack.setConstruction_unit(null);
            }
        }
        return progressTrackMapper.queryByNameAndUnit(progressTrack);
    }

    /**
     * 新增数据
     *
     * @param simple
     * @param projectTeam
     * @return
     */
    @Override
    public ResultInfo json(String simple, String projectTeam) {
        List<String> keys = progressTrackMapper.selectColumnKeyResult();
        List<String> values = progressTrackMapper.selectColumnValueResult();
        JSONArray array = JSONArray.parseArray(simple);
        for (int i = 0; i < array.size(); i++) {
            String value = String.valueOf(array.get(i));
            Map map = (Map) JSON.parse(value);
            Set<String> set = map.keySet();
            String code = "";
            String key1 = "";
            String values1 = "";
            String sql = "";
            for (String key : set) {
                if (values.contains(key)) {
                    String val = String.valueOf(map.get(key));
                    if (keys.get(values.indexOf(key)).equals("code")) {
                        code = String.valueOf(map.get(key));
                        val = code;
                    }
                    if (key1.equals("")) {
                        key1 += keys.get(values.indexOf(key));
                    } else {
                        key1 += "," + keys.get(values.indexOf(key));
                    }
                    if (values1.equals("")) {
                        values1 += "'" + val + "'";
                    } else {
                        values1 += ",'" + val + "'";
                    }
                }
            }
            List<ProgressTrack> list = progressTrackMapper.selectByCode(code);
            if (list.size() == 0) {
                String id = UUIDBuild.getUUID();
                sql = "insert into pms_capital(id," + key1 + ",project_team) values ('" + id + "'," + values1 + ",'" + projectTeam + "')";
                progressTrackMapper.insertObjectBatch(sql);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }


}
